package cj.web.xh.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.admin.utils.DateUtils;
import cj.web.login.controllers.BaseController;
import cj.web.xh.domain.Member;
import cj.web.xh.service.MemberService;

@Path("/member")
public class MemberController extends BaseController{

	private static Logger logger = Logger.getLogger(MemberController.class.getName());
	
	@Autowired
	private MemberService memberService;
	
	@Get("show")
	public String show(Model model) {
		return "/views/xh/memberList.vm";
	}

	@Post("getList")
	public String queryList(Member member) {
		//当前页  
		member.setStart((member.getPage()-1)*member.getRows());
		member.setLength(member.getRows());
		
		JSONObject jo = new JSONObject();
		
		Map rep = memberService.query(member);
		List<Member> objs = (List<Member>) rep.get("objLis");
		int size = (int) rep.get("size");
		
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		return "/views/xh/memberDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Member member,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		member.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		member.setCreateAt(DateUtils.getNowTime());
		member.setCreateUser(returnSessionObj(request).getAccount());
		member.setUpdateAt(DateUtils.getNowTime());
		member.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			memberService.create(member);
		}catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		Member member = memberService.queryById(id);

		model.add("member", member);
		return "/views/xh/memberEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Member member,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		member.setUpdateAt(DateUtils.getNowTime());
		member.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			memberService.update(member);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			memberService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}